import { ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { useCookies } from '@vueuse/integrations/useCookies'
import { router } from '@/core/router'
export function useTabList() {
  const route = useRoute()
  const cookie = useCookies()

  const activeTab = ref(route.path)
  const tabList = ref([
    {
      title: '后台首页',
      path: '/',
    },
  ])

  // 添加标签导航
  function addTab(tab) {
    const noTab = tabList.value.findIndex((t) => t.path === tab.path) === -1
    if (noTab) {
      tabList.value.push(tab)
    }

    cookie.set('tabList', tabList.value)
  }

  // 初始化标签导航列表
  function initTabList() {
    const tbs = cookie.get('tabList')
    if (tbs) {
      tabList.value = tbs
    }
  }

  initTabList()

  onBeforeRouteUpdate((to) => {
    activeTab.value = to.path
    addTab({
      title: to.meta.title,
      path: to.path,
    })
  })

  const changeTab = (t) => {
    console.log(route.path)
    activeTab.value = t
    router.push(t)
  }

  const removeTab = (remoteTab) => {
    const tabs = tabList.value
    let currentActive = activeTab.value
    if (currentActive === remoteTab) {
      tabs.forEach((tab, index) => {
        if (tab.path === remoteTab) {
          // 去拿下一个tab 没有就返回上一个
          const nextTab = tabs[index + 1] || tabs[index - 1]
          if (nextTab) {
            currentActive = nextTab.path
          }
        }
      })
    }

    activeTab.value = currentActive
    tabList.value = tabList.value.filter((tab) => tab.path != remoteTab)

    cookie.set('tabList', tabList.value)
  }

  const handleClose = (c) => {
    if (c === 'clearAll') {
      // 切换回首页
      activeTab.value = '/'
      // 过滤只剩下首页
      tabList.value = [
        {
          title: '后台首页',
          path: '/',
        },
      ]
    } else if (c === 'clearOther') {
      // 过滤只剩下首页和当前激活
      tabList.value = tabList.value.filter(
        (tab) => tab.path === '/' || tab.path === activeTab.value
      )
    }
    cookie.set('tabList', tabList.value)
  }

  return {
    activeTab,
    tabList,
    changeTab,
    removeTab,
    handleClose,
  }
}
